// sync_panda_tables_get_arg.h
//

#if !defined(GET_ARG_H)
#define GET_ARG_H

#include <string>
//#include <iosfwd>
#include <sstream>
#include <iostream>
#include <fstream>

// adhoc solution to output to two streams at once
struct dual_streams
{
	dual_streams(std::ostream& os1_, std::ostream& os2_)
		: os1(os1_), os2(os2_){}
	dual_streams(const dual_streams& ds)
		: os1(ds.os1), os2(ds.os2){}
	std::ostringstream 		strm_;
	std::ostringstream&		strm() {return strm_;};
	std::ostream&			os1;
	std::ostream&			os2;
	~dual_streams()
	{
		if (strm_.str().length())
		{
			os1 		<< strm_.str();
			os2 		<< strm_.str();
		}
	}
};

class t_program_args
{
public:

	
	bool			vacuum;
	bool			verbose;

	// text description of the current operation
	std::string		description;

	
	// list of sql commands
	std::vector<std::string>
					sql_commands;

	// database login string
	std::string		db_login;
	
	// for error / progress messages etc.
	dual_streams	CERR();
	dual_streams	CLOG();
	std::ostream&	VERBOSE() {return verbose ? std::cout : strm_ignore;}
	std::ostream&	IGNORE() {return strm_ignore;}

	// for initializing this struct
	friend
	bool GetArguments(int argc, char* argv[], t_program_args& args);
private:
	std::ostringstream 	strm_ignore;
	bool				log_to_file;
	std::ofstream		f_err_log;
};

// output to cerr and logfile if there is one
inline dual_streams t_program_args::CERR()
{
	return dual_streams(
						log_to_file							   	?
						static_cast<std::ostream&>(f_err_log)	:
						static_cast<std::ostream&>(strm_ignore), std::cerr	);
}

// output to cout if verbose and logfile if there is one
inline dual_streams t_program_args::CLOG()
{
	return dual_streams(
						log_to_file									?
						static_cast<std::ostream&>(f_err_log)				:
						static_cast<std::ostream&>(strm_ignore), std::cout	);
}

bool GetArguments(int argc, char* argv[], t_program_args& args);
#endif //SYNC_PANDA_TABLES_GET_ARG_H
